/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月29日
 */
package com.massyframework.beanskin.runtime.modular;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import com.massyframework.beanskin.ModuleIdentifier;
import com.massyframework.beanskin.ModuleRepository;
import com.massyframework.beanskin.RequireLibraryDescriptor;

/**
 * 初始化参数配置项目构建器
 * @author huangkh
 *
 */
public class InitParamsBuilder extends AbstractModuleBuilder {

	protected Map<String, String> params;
	
	/**
	 * 无参构造方法
	 */
	public InitParamsBuilder() {
		super();
	}

	/**
	 * 使用{@code id}创建新实例
	 * <p>
	 * 适用于可热拔插模块，保持原有编号
	 * @param id {@link Integer}
	 */
	protected InitParamsBuilder(Integer id) {
		super(id);
	}

	/**
	 * 设置参数
	 * <p>
	 * 不支持覆写已存在参数.
	 * @param name {@link String},参数名
	 * @param value {@link String},参数值
	 * @return {@link InitParamsBuilder}
	 */
	public InitParamsBuilder setParameter(String name, String value) {
		return this.setParameter(name, value, null);
	}
	
	/**
	 * 设置参数
	 * <p>
	 * 不支持覆写已存在参数.
	 * @param name {@link String},参数名
	 * @param value {@link String},参数值
	 * @param action {@link Consumer},参数设置是否成功的回调函数,可以为null.
	 * @return {@link InitParamsBuilder}
	 */
	public InitParamsBuilder setParameter(String name, String value, Consumer<Boolean> action) {
		if (name != null && value != null) {
			if (this.params == null) {
				this.params = new HashMap<>();
			}
			boolean success = this.params.putIfAbsent(name, value) == null;
			if (action != null) {
				action.accept(success);
			}
		}
		
		return this;
	}

	@Override
	public InitParamsBuilder identifier(ModuleIdentifier value) {
		super.identifier(value);
		return this;
	}

	@Override
	public InitParamsBuilder name(String value) {
		super.name(value);
		return this;
	}

	@Override
	public InitParamsBuilder description(String value) {
		super.description(value);
		return this;
	}
	
	
	
	@Override
	public InitParamsBuilder decryption(String publicKey, String x509Name, String x509Encrypted,
			List<String> classNames, List<String> scriptFileNames) {
		super.decryption(publicKey, x509Name, x509Encrypted, classNames, scriptFileNames);
		return this;
	}

	@Override
	public InitParamsBuilder addRequireLibrary(RequireLibraryDescriptor value) {
		super.addRequireLibrary(value);
		return this;
	}

	@Override
	public InitParamsBuilder moduleRepository(ModuleRepository value) {
		super.moduleRepository(value);
		return this;
	}
	
	
}
